package com.huika.dealontop.views;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.huika.dealontop.R;
import com.huika.dealontop.utils.UIUtils;

/**
 * Author: Victor Wu <br/>
 * Email: victor-wu*foxmail.com <br/>
 * Time: 2015/11/30 13:17 <br/>
 * Description: TODO
 */
public class ModifiedGenderDialog extends IOSAlertDialog{

    private ImageView mMaleTick;
    private ImageView mFemaleTick;

    private Gender gender;

    public void setOnGenderChangedListener(OnGenderChangedListener changedListener) {
        this.changedListener = changedListener;
    }

    private OnGenderChangedListener changedListener;

    public ModifiedGenderDialog(Context context) {
        super(context);
        setCanceledOnTouchOutside(true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View customView = View.inflate(UIUtils.getContext(), R.layout.modified_gender_dialog, null);
        customView.findViewById(R.id.male_item).setOnClickListener(this);
        customView.findViewById(R.id.female_item).setOnClickListener(this);
        mMaleTick = (ImageView) customView.findViewById(R.id.iv_select_male);
        mFemaleTick = (ImageView) customView.findViewById(R.id.iv_select_female);
        setConfirmClickListener(new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                if (changedListener != null) {
                    changedListener.onGenderChanged(gender);
                }
                dismiss();
            }
        });
        setCustomView(customView);
        updateSelect(gender);
    }

    @Override
    public void onClick(View view) {
        int id  = view.getId();
        if(id == R.id.male_item){
            updateSelect(Gender.MALE);
        }else if(id == R.id.female_item){
            updateSelect(Gender.FEMALE);
        }
        super.onClick(view);
    }

    public void updateSelect(Gender GENDER){
        gender = GENDER;
        if(mMaleTick != null){
            mMaleTick.setVisibility(gender == Gender.MALE ? View.VISIBLE : View.GONE);
        }
        if(mFemaleTick != null){
            mFemaleTick.setVisibility(gender == Gender.FEMALE ? View.VISIBLE : View.GONE);
        }
    }

    public void setGender(Gender gender) {
        updateSelect(gender);
    }

    public Gender getGender() {
        return gender;
    }


    public enum Gender{
        MALE(1),FEMALE(2);
        public int value;
        Gender(int value){
            this.value = value;
        }
    }

    public interface OnGenderChangedListener{
        void onGenderChanged(Gender gender);
    }
}
